<template>
	<view>
		<view class="topsMy  bgw p30">
			<u-tabs :list="list" :is-scroll="false" :current="current" active-color="#1BBF80" @change="change"></u-tabs>
		</view>
		<view class="contents">
			<view>
				<view @tap="GoUsers(1,v)" v-if="current ==0" v-for="(v,i) in data" :key="i" class="listFansBox">
					<view class="listFans">
						<image class="userImg" :src="v.toMember.eperAccount.eperAvatarImg" mode=""></image>
						<view class="userTxt">
							<p class="p1 one">
								{{v.toMember.eperAccount.eperUsername}}
							</p>
							<p class="p2 one">
								粉丝 {{v.toMember.fansQuantity}}
							</p>
						</view>
						<view @tap.stop @tap="removes(v,i)" class="pinkLabel">
							<image class="addIcons" src="../../static/image/icon_duihao@2x.png" mode=""></image>
							已关注
						</view>
					</view>
				</view>
				<view @tap="GoUsers(2,v)" v-if="current ==1" v-for="(v,i) in data" :key="i" class="listFansBox">
					<view class="listFans">
						<image class="userImg" :src="v.fromMember.eperAccount.eperAvatarImg" mode=""></image>
						<view class="userTxt">
							<p class="p1 one">
								{{v.fromMember.eperAccount.eperUsername}}
							</p>
							<p class="p2 one">
								粉丝 {{v.fromMember.fansQuantity}}
							</p>
						</view>
						<view @tap.stop @tap="removes(v,i)" v-if="v.fromMember.isFollow  == true" class="pinkLabel">
							<image class="addIcons" src="../../static/image/icon_duihao@2x.png" mode=""></image>
							已关注
						</view>
						<view @tap.stop @tap="likeAdd(v,i)" v-if="v.fromMember.isFollow  == false" class="pinkLabel pinkLabels">
							<image class="addIcons" src="../../static/image/addwhite.png" mode=""></image>
							关注
						</view>
					</view>
				</view>
				<view v-if="data == '' " class="dataNone">
					<image src="../../static/image/guanzhu@2x.png" mode=""></image>
					<p>您还没有关注的人哦</p>
				</view>
			</view>
		</view>
		<models @success="delect" ref="model" title="提示" msg="确认取消关注"></models>
	</view>
</template>

<script>
	import waterfall from '@/components/waterfall/waterfall.vue'
	import models from '../../components/models/models.vue'
	import {
		fansList,
		followList,
		followAdd,
		followCancel
	} from '../../api/api.js'
	export default {
		components: {
			waterfall
		},
		data() {
			return {
				data: [],
				state: 0,
				list: [{
					name: '关注'
				}, {
					name: '粉丝'
				}],
				current: 0,
				pageNumber: 1,
				pageSize: 10,
				stops: 0,
				delects: []
			};
		},
		onLoad(e) {
			this.state = e.state
			this.current = e.state
			this.swiperCurrent = e.state
			this.init()
		},
		onReachBottom() {
			this.checking()
		},
		methods: {
			GoUsers(type, v) {
				let id;
				if (type == 1) {
					  id = v.toMember.id
				} else {
					  id = v.fromMember.id
				}
				this.$navigateTo('../users/users?id=' + id)
			},
			async delect() {
				let id = 0
				console.log(this.delects);
				if (this.current == 0) {
					id = this.delects.toMember.id
				} else {
					id = this.delects.fromMember.id
				}
				let data = await followCancel({
					id: id
				})
				let i = this.delects.i
				if (data.code == 0) {
					if (this.current == 0) {
						console.log(i);
						this.data.splice(i, 1)
					} else {
						this.data[i].fromMember.isFollow = false
					}
				}

			},
			removes(v, i) {
				this.delects = v
				this.delects.i = i
				this.$refs['model'].show()
			},
			async likeAdd(v, i) {
				let data = await followAdd({
					id: v.fromMember.id
				})
				if (data.code == 0) {
					this.data[i].fromMember.isFollow = true
				}
			},
			change(index) {
				this.data = []
				this.stops = 0
				this.pageNumber = 1
				this.current = index;
				this.init()
			},
			checking() {
				if (this.stops == 0) {
					this.init()
				}
			},
			async init() {
				let parameter = {
					pageable: {
						pageNumber: this.pageNumber,
						pageSize: this.pageSize
					}
				}
				let data = []
				if (this.current == 0) {
					data = await followList(parameter)
				} else {
					data = await fansList(parameter)
				}
				data = data.data.content
				if (data.length != this.pageSize) {
					this.stops = 1
				}
				this.data = [...this.data, ...data]
				this.pageNumber = this.pageNumber + 1
			},
		}
	}
</script>

<style lang="less" scoped>
	@import './index.less';
</style>
